/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cxhd.pacs.controller.user;

import com.cxhd.pacs.model.FileDetail;
import com.cxhd.pacs.model.HostDetail;
import com.cxhd.pacs.model.LisDetail;
import com.cxhd.pacs.tools.FileUtil;
import com.cxhd.pacs.tools.ImageRender;
import com.cxhd.pacs.tools.PDFRender;
import com.jfinal.core.Controller;
import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import java.io.File;
import java.io.FileFilter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author 52461
 */
public class PatDataCtrl extends Controller {

    public void index() {
        setAttr("pat_id", getPara("pat_id"));
       
        setAttr("name", getPara("name"));
        setAttr("sex", getPara("sex"));
        setAttr("age", getPara("age"));
        render("/view/pat/main.jsp");
    }

    public void bt() {
      
        String count ="";
        //Record re = Db.findFirst("select count(*) as count from pat_data where info_id='" + getPara("pat_id") + "' AND  name='" + getPara("name") + "' and sex = '" + unescape(getPara("sex")) + "' ");
         Record re = Db.findFirst("select count(*) as count from pat_data where info_id='" + getPara("pat_id") + "' ");
        
        if(re!=null&&re.get("count")!=null){
        count = re.get("count")+"";
        }
        setAttr("count", count);
        setAttr("url", getRequest().getContextPath()+"/pat?pat_id="+getPara("pat_id")+"&name="+getPara("name")+"&sex="+getPara("sex"));
        render("/view/pat/count.jsp");
    }

    public void getItem() {
        String sql = " select * ";
        String param = " from  pat_data ";
        if (getPara("pat_id") != null && !getPara("pat_id").equalsIgnoreCase("")) {
//            param = " from  pat_data "
//                    + " where info_id='" + getPara("pat_id") + "' AND  name='" + getPara("name") + "' and sex = '" + getPara("sex") + "' ";
              param = " from  pat_data "
                    + " where info_id='" + getPara("pat_id") + "'";
            Page<Record> userPage = Db.paginate(getParaToInt("currentPage", 1),
                    getParaToInt("pageSize", 10), sql, param);
            System.out.println(sql + param);
            renderJson(userPage);
        } else {
            renderText(false + "");
        }

    }

    public void result() {
        Record re = Db.findFirst("select * from pat_data where pk = " + getPara("pk"));
        setAttr("result", re.get("event_result"));
//        if (re.getStr("result_type").equalsIgnoreCase("html")) {
//            //re.remove("event_result");
//        }
        setAttr("item", JsonKit.toJson(re));
        render("/view/pat/result.jsp");
    }

    public void chart() {
        setAttr("pat_id", getPara("pat_id"));
        setAttr("name", getPara("name"));
        setAttr("sex", getPara("sex"));
        setAttr("age", getPara("age"));
        // setAttr("item",JsonKit.toJson(Db.findFirst("select * from pat_data where pk = " + getPara("pk"))));
        render("/view/pat/chart.jsp");
    }

    public void getChart() {
        renderJson(Db.find("select event_name as name,COUNT(*) as count from pat_data where info_id='" + getPara("pat_id") + "' AND  name='" + getPara("name") + "' and sex = '" + getPara("sex") + "' group by event_name "));
    }

    public void getImage() {
        String fileId = getPara("fileId");
        FileDetail mo = FileDetail.dao.getFile(fileId);
        File myFile = null;
        System.out.println(mo.getStr("type"));

        myFile = new File(mo.getStr("base_path") + "/" + mo.getStr("path"));

        System.out.println(myFile.getAbsoluteFile());
        render(new ImageRender(myFile, ""));

    }

    public void getPdf() {
        String fileId = getPara("fileId");
        FileDetail mo = FileDetail.dao.getFile(fileId);
        File myFile = null;
        System.out.println(mo.getStr("type"));

        myFile = new File(mo.getStr("base_path") + "/" + mo.getStr("path"));

        System.out.println(myFile.getAbsoluteFile());
        render(new PDFRender(myFile, ""));

    }

    public void getVideo() {
        String fileId = getPara("fileId");
        FileDetail mo = FileDetail.dao.getFile(fileId);
        File myFile = null;
        System.out.println(mo.getStr("type"));

        myFile = new File(mo.getStr("base_path") + "/" + mo.getStr("path"));

        System.out.println(myFile.getAbsoluteFile());
        render(new PDFRender(myFile, ""));

    }

    public void getLisResult() {

        renderJson(LisDetail.dao.getDetail(getPara("pk")));

    }

    public void getResult() {
        String event_result = Db.findFirst("select * from pat_data where pk=" + getPara("pk")).getStr("event_result");
        
        System.out.println(event_result+"   "+event_result.length());
        if (event_result != null && event_result.length() < 20) {
            String fileId = event_result;
            FileDetail mo = FileDetail.dao.getFile(fileId);
            File myFile = null;
            System.out.println(mo.getStr("type"));
            
            myFile = new File(mo.getStr("base_path") + "/" + mo.getStr("path"));
            renderText(FileUtil.outWriter(myFile.getAbsolutePath()));
        } else {
            renderText(event_result);
        }
         //renderText("false");
        //renderText(Db.findFirst("select * from pat_data where pk=" + getPara("pk")).getStr("event_result"));
    }
    
     public void getPdfToImage() {
        String fileId = getPara("fileId");
        FileDetail fd = new FileDetail().getFile(fileId);
        String path = fd.get("base_path") + "/" + fd.get("path") + "";
        System.out.println(path);
        File[] fl = new File(path).getParentFile().listFiles(new FileFilter() {

            public boolean accept(File f) {
                if (f.getName().endsWith(".jpg")) {
                    return true;
                } else {
                    return false;
                }
            }
        });
        List<String> listf = new ArrayList<String>();
        if (fl != null && fl.length > 0) {
            for (File f : fl) {
                listf.add(f.getAbsolutePath());
            }
        }
        renderJson(listf);
    }

    public void getPdfImage() {
        render(new ImageRender(new File(getPara("path")), null));
    }
    
    
    public static String unescape(String src) {
        StringBuffer tmp = new StringBuffer();
        tmp.ensureCapacity(src.length());
        int lastPos = 0, pos = 0;
        char ch;
        while (lastPos < src.length()) {
            pos = src.indexOf("%", lastPos);
            if (pos == lastPos) {
                if (src.charAt(pos + 1) == 'u') {
                    ch = (char) Integer.parseInt(src.substring(pos + 2, pos + 6), 16);
                    tmp.append(ch);
                    lastPos = pos + 6;
                } else {
                    ch = (char) Integer.parseInt(src.substring(pos + 1, pos + 3), 16);
                    tmp.append(ch);
                    lastPos = pos + 3;
                }
            } else {
                if (pos == -1) {
                    tmp.append(src.substring(lastPos));
                    lastPos = src.length();
                } else {
                    tmp.append(src.substring(lastPos, pos));
                    lastPos = pos;
                }
            }
        }
        return tmp.toString();
    }
}
